package com.guaju.yueshangLife.activity;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.githang.statusbar.StatusBarCompat;
import com.guaju.yueshangLife.R;
import com.guaju.yueshangLife.base.MyAppcomActivity;
import com.guaju.yueshangLife.bean.CodeLoginEvent;
import com.guaju.yueshangLife.databinding.ActivityLoginBinding;
import com.guaju.yueshangLife.utils.PreferenceUtil;
import com.guaju.yueshangLife.utils.ProgressUtil;
import com.guaju.yueshangLife.viewmodel.login.LoginVM;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class LoginActivity extends MyAppcomActivity {

    private ActivityLoginBinding binding;
    private boolean isPasswordType = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_login);
        //一体栏
        StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.home_background));
        LoginVM loginVM = new LoginVM();
        binding.setViewmodel(loginVM);
        initView();
    }

    private void initView() {
        binding.etPassword.addTextChangedListener(new LoginTextWatch());
        setListener();
    }

    private void setListener() {
        binding.flBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        binding.flEye.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isPasswordType) {
                    binding.etPassword.setInputType(InputType.TYPE_TEXT_VARIATION_NORMAL);
                    binding.ivEye.setBackgroundResource(R.mipmap.eye_open);
                    isPasswordType = false;
                } else {
                    binding.etPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    binding.ivEye.setBackgroundResource(R.mipmap.eye_close);
                    isPasswordType = true;
                }
            }
        });
    }

    public class LoginTextWatch implements TextWatcher {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (s.toString().length() > 0) {
//                if (Build.VERSION.SDK_INT>=21)
//                    binding.btLogin.setBackgroundTintList(getResources().getColorStateList(R.color.selector_login_button));
//                else{
                binding.btLogin.setBackgroundResource(R.drawable.selector_login_button);
//                }
            } else {
                binding.btLogin.setBackgroundResource(R.drawable.shape_login_unable_bg);
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(CodeLoginEvent event) {
        int code = event.getCode();
        String msg = event.getMsg();
        String token_type = event.getToken_type();
        String access_token = event.getAccess_token();

        if (500 == code) {
            ProgressUtil.dismissProgressDialog();
            Toast.makeText(LoginActivity.this, msg, Toast.LENGTH_SHORT).show();
        } else if (200 == code) {
            Toast.makeText(LoginActivity.this, msg, Toast.LENGTH_SHORT).show();
            ProgressUtil.dismissProgressDialog();
            PreferenceUtil.setTokenType(token_type);
            PreferenceUtil.setToken(access_token);
            PreferenceUtil.setIsLogin(true);
            String trim = binding.etPhonenum.getText().toString().trim();
            PreferenceUtil.setFphone(trim);
            finish();
        } else {
            ProgressUtil.dismissProgressDialog();
            Toast.makeText(LoginActivity.this, msg, Toast.LENGTH_SHORT).show();
        }
        binding.btLogin.setClickable(true);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
